package com.sskj.gocoin.v.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.bulong.rudeness.RudenessScreenHelper;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.sskj.gocoin.R;
import com.sskj.gocoin.R2;
import com.sskj.gocoin.common.App;
import com.sskj.gocoin.common.ClassicsHeader;
import com.sskj.gocoin.common.adapter.BaseAdapter;
import com.sskj.gocoin.common.adapter.ViewHolder;
import com.sskj.gocoin.common.decoration.DividerLineItemDecoration;
import com.sskj.gocoin.m.bean.RechargeListBean;
import com.sskj.gocoin.p.fragment.CapitalDetailsPresenter;
import com.sskj.gocoin.utils.DateUtil;
import com.sskj.gocoin.v.activity.CapitalDetailsActivity;
import com.sskj.gocoin.v.base.BaseFragment;
import com.sskj.lib.util.TipUtil;

import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * ProjectName：new_bfex
 * DESC: (资金管理-->资金明细)
 * Created by 李岩 on 2018/7/11 0011
 * updateName:(修改人名称)
 * updateTime:(修改时间)
 * updateDesc:(修改内容)
 */
public class CapitalDetailsFragment extends BaseFragment<CapitalDetailsActivity, CapitalDetailsPresenter> {
    @BindView(R2.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R2.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R2.id.date_show)
    LinearLayout dateShow;
    Unbinder unbinder;
    private BaseAdapter<RechargeListBean> mAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_recyclerview;
    }

    @Override
    public CapitalDetailsPresenter getPresenter() {
        return new CapitalDetailsPresenter();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        dateShow.setVisibility(View.GONE);
        SparseArray<String> stateMap = new SparseArray<>();
        stateMap.put(5, App.INSTANCE.getString(R.string.strCapitalDetailsFragment230));
        stateMap.put(4, App.INSTANCE.getString(R.string.strCapitalDetailsFragment208));
        stateMap.put(3, App.INSTANCE.getString(R.string.strCapitalDetailsFragment209));
        stateMap.put(2, App.INSTANCE.getString(R.string.strCapitalDetailsFragment210));
        stateMap.put(1, App.INSTANCE.getString(R.string.strCapitalDetailsFragment211));
        int index = getArguments().getInt("index", -1);
        int assetId = getArguments().getInt("assetId", -1);
        String coin_type = getArguments().getString("coin_type");
        mRefreshLayout.setRefreshHeader(new ClassicsHeader(mActivity));

        mRefreshLayout.setOnRefreshListener(refreshlistener -> {
            refreshlistener.finishRefresh();
            switch (index) {
                case 0:
                    mPresenter.getRechargeList(assetId, true);
                    break;
                case 1:
                    mPresenter.getWithdrawList(assetId, true);
                    break;
                case 2:
                    if (assetId == 0) {
                        mPresenter.getBalanceRecordList(true);
                    } else {
                        mPresenter.getAssetRecordList(assetId, true);
                    }
                    break;
            }
        });
        mRefreshLayout.setOnLoadMoreListener(loadmorelistener -> {
            loadmorelistener.finishLoadMore();
            switch (index) {
                case 0:
                    mPresenter.getRechargeList(assetId, false);
                    break;
                case 1:
                    mPresenter.getWithdrawList(assetId, false);
                    break;
                case 2:
                    if (assetId == 0) {
                        mPresenter.getBalanceRecordList(false);
                    } else {
                        mPresenter.getAssetRecordList(assetId, false);
                    }
                    break;
            }
        });
        if (index == 0) {
            mRefreshLayout.autoRefresh();
        }
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
//        mRecyclerView.addItemDecoration(new DividerItemDecoration(mActivity, LinearLayoutManager.VERTICAL));

        mRecyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setFirstDraw(false)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(getActivity(), 20))
                .setDividerColor(ContextCompat.getColor(getContext(), R.color.transparent)));
        if (index == 2) {
            mAdapter = new BaseAdapter<RechargeListBean>(R.layout.item_capital_details_asset, null, mRecyclerView) {
                @Override
                public void bind(ViewHolder holder, RechargeListBean item) {
                    holder.setText(R.id.item_capital_asset_count, item.getPrice() + " " + coin_type);
                    holder.setText(R.id.item_capital_asset_time, DateUtil.getDateFormat(item.getAddtime() * 1000, DateUtil.PATTERN_J));
                    holder.setText(R.id.item_capital_asset_remark, TextUtils.isEmpty(item.getMemo()) ? item.getType() : item.getMemo());
                }
            };
        } else {
            mAdapter = new BaseAdapter<RechargeListBean>(R.layout.item_capital_details_new, null, mRecyclerView) {

                @Override
                public void bind(ViewHolder holder, RechargeListBean item) {
                    if (item.getState() == 1) {
                        holder.setBackgroundRes(R.id.item_capotal_details_status, R.drawable.lib_button_cancel_87a);
                        holder.getView(R.id.item_capotal_details_status)
                                .setOnClickListener(v -> {
                                    TipUtil.getTip(getActivity(), getString(R.string.minefabi_cancel), () -> {
                                        mPresenter.cancelTibi(item.getCash_id());
                                    }).show();
                                });
                    } else {
                        holder.setBackgroundColor(R.id.item_capotal_details_status, Color.TRANSPARENT);
                        holder.getView(R.id.item_capotal_details_status)
                                .setOnClickListener(v -> {
                                });
                    }

                    holder.setTextColor(R.id.item_capotal_details_status, getResources().getColor(item.getState() == 3 ? R.color.market_up : item.getState() == 4 ? R.color.market_down : R.color.textColorNormal));
                    if (item.getState() == 1) {
                        holder.setTextColor(R.id.item_capotal_details_status, getResources().getColor(R.color.minefabi_87a));

                    }
                    switch (index) {
                        case 0:
                            holder.setText(R.id.item_capotal_details_withdraw_address_key, App.INSTANCE.getString(R.string.cbaddress) + "\t");
                            holder.setText(R.id.item_capotal_details_withdraw_address, item.getChongzhi_url());
                            holder.setText(R.id.item_capotal_details_submit_count_key, App.INSTANCE.getString(R.string.cbnum) + "\t");
                            holder.setText(R.id.item_capotal_details_status, item.getState() == 1 ? App.INSTANCE.getString(R.string.strCapitalDetailsFragment204) : App.INSTANCE.getString(R.string.strCapitalDetailsFragment205));
                            break;
                        case 1:
                            holder.setText(R.id.item_capotal_details_withdraw_address_key, App.INSTANCE.getString(R.string.tbaddress) + "\t");
                            holder.setText(R.id.item_capotal_details_withdraw_address, item.getQianbao_url());
                            holder.setText(R.id.item_capotal_details_submit_count_key, App.INSTANCE.getString(R.string.tbnum) + "\t");
                            holder.setText(R.id.item_capotal_details_status, stateMap.get(item.getState()));
                            break;
                    }
                    if (item.getState() == 3) {
                        holder.setText(R.id.item_capotal_details_err_hint, item.getMemo());
                        holder.setGone(R.id.item_capotal_details_err_hint, true);
                    } else {
                        holder.setGone(R.id.item_capotal_details_err_hint, false);
                    }
                    if (item.getState() == 1 || item.getState() == 2) {
                        holder.setGone(R.id.llCheckTime, false);
                    } else {
                        holder.setGone(R.id.llCheckTime, true);

                    }
                    holder.setText(R.id.item_capotal_details_submit_count, item.getPrice());
                    holder.setText(R.id.item_capotal_details_start_time, DateUtil.getDateFormat(new Date(item.getAddtime() * 1000), DateUtil.PATTERN_K));
                    holder.setText(R.id.item_capotal_details_end_time, TextUtils.isEmpty(item.getCheck_time()) ? "" : DateUtil.getDateFormat(new Date(Long.valueOf(item.getCheck_time()) * 1000), DateUtil.PATTERN_K));

                }
            };
        }

//        View inflate = LayoutInflater.from(mActivity).inflate(R.layout.empty_view, null);
//        mAdapter.addHeaderView(inflate);
        mAdapter.setEmtyView();
    }

    public void onRechargeRecordSuccess(List<RechargeListBean> data, boolean isRefresh) {
        if (isRefresh) {
            mAdapter.setNewData(data);
        } else {
            mAdapter.addData(data);
        }
    }

    public void onWithdrawRecordSuccess(List<RechargeListBean> data, boolean isRefresh) {
        if (isRefresh) {
            mAdapter.setNewData(data);
        } else {
            mAdapter.addData(data);
        }
    }

    public void onAssetRecordSuccess(List<RechargeListBean> res, boolean isRefresh) {
        if (isRefresh) {
            mAdapter.setNewData(res);
        } else {
            mAdapter.addData(res);
        }
    }

    public void update() {
        if (mAdapter.getData().size() <= 0) {
            mRefreshLayout.autoRefresh();
        }
    }


    public void removeSuccess() {
        mRefreshLayout.autoRefresh();
    }
}
